/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.metadata.rtcustomization.common;

import com.inspur.edp.lcm.metadata.common.ServiceUtils;
import com.inspur.edp.lcm.metadata.common.configuration.MetadataServiceHelper;
import com.inspur.edp.metadata.rtcustomization.api.entity.config.CustomizationConfiguration;

import java.util.List;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 自定义配置加载器
 *
 * @author zhaoleitr
 */
public class CustomizationConfigLoader {

    // 文件名称
    public static String fileName = "config/platform/common/lcm_rtcustomization.json";

    // 自定义配置
    private static String sectionName = "CustomizationConfiguration";

    // 绝对路径
    private String absoluteFilePath;

    /**
     * 获取绝对路径
     *
     * @return
     */
    public String getAbsoluteFilePath() {
        if (StringUtils.isEmpty(absoluteFilePath)) {
            absoluteFilePath = ServiceUtils.getAbsoluteConfigFilePath(fileName);
        }
        return absoluteFilePath;
    }

    private List<CustomizationConfiguration> customizationConfigurations;

    /**
     * 构造函数
     */
    public CustomizationConfigLoader() {
        loadCustomizationConfigurations();
    }

    /**
     * 加载自定义配置
     *
     * @return
     */
    protected List<CustomizationConfiguration> loadCustomizationConfigurations() {
        try {
            if (CollectionUtils.isEmpty(customizationConfigurations)) {
                customizationConfigurations = MetadataServiceHelper.getConfigurationList(getAbsoluteFilePath(),
                        sectionName, CustomizationConfiguration.class);
            }
            return customizationConfigurations;
        } catch (Exception e) {
            throw new RuntimeException("读取配置文件失败" + fileName, e);
        }
    }

    /**
     * 获取自定义配置数据
     *
     * @param typeName
     * @return
     */
    protected CustomizationConfiguration getCustomizationConfigurationData(String typeName) {
        loadCustomizationConfigurations();
        if (customizationConfigurations != null && customizationConfigurations.size() > 0) {
            for (CustomizationConfiguration data : customizationConfigurations) {
                if (data.getCommon().getTypeCode().equalsIgnoreCase(typeName)) {
                    return data;
                }
            }
        } else {
            return null;
        }
        return null;
    }
}
